import { ElMessage, ElMessageBox, ElNotification, ElLoading } from 'element-plus'

let loadingInstance

export function msg(content, options = {}) {
  options = options || {}
  options.message = content
  options.type = options.type || 'info'
  if (!('showClose' in options)) options.showClose = true
  return ElMessage(options)
}

export function msgSuccess(content, close = () => {}) {
  return msg(content, { type: 'success', onClose: close })
}
export function msgWarning(content, close = () => {}) {
  return msg(content, { type: 'warning', onClose: close })
}

export function msgError(content, close = () => {}) {
  return msg(content, { type: 'error', onClose: close })
}

export function alert(content, title = '系统提示', options = {}) {
  options = options || {}
  options.confirmButtonClass = options?.confirmButtonClass || 'message-box-confirm_btn'
  return ElMessageBox.alert(content, title, options)
}

export function alertError(content, title = '系统提示', options = {}) {
  return alert(content, title, { type: 'error', ...options })
}

export function alertSuccess(content, title = '系统提示', options = {}) {
  return alert(content, title, { type: 'success', ...options })
}

export function alertWarning(content, title = '系统提示', options = {}) {
  return alert(content, title, { type: 'warning', ...options })
}

export const confirm = (content, title = '系统提示', options = {}) => {
  options = options || {}
  options.confirmButtonClass = options?.confirmButtonClass || 'message-box-confirm_btn'
  options.type = options?.type || 'warning'
  return ElMessageBox.confirm(content, title, options)
}

export const prompt = (content, title = '系统提示', options = {}) => {
  options = options || {}
  options.confirmButtonClass = options?.confirmButtonClass || 'message-box-confirm_btn'
  options.type = options?.type || 'warning'
  return ElMessageBox.prompt(content, title, options)
}

export function loading(content) {
  loadingInstance = ElLoading.service({
    lock: true,
    text: content,
    background: 'rgba(0, 0, 0, 0.4)',
    spinner: 'el-icon-loading'
  })
  return loadingInstance
}

export function closeLoading() {
  if (loadingInstance) loadingInstance.close()
}

export function notify(content, title = '', options = {}) {
  options = options || {}
  options.title = title
  options.message = content
  options.type = options.type || 'info'
  return ElNotification(options)
}

export function notifyError(content, title = '', options = {}) {
  return notify(content, title, { type: 'error', ...options })
}

export function notifySuccess(content, title = '', options = {}) {
  return notify(content, title, { type: 'success', ...options })
}

export function notifyWarning(content, title = '', options = {}) {
  return notify(content, title, { type: 'warning', ...options })
}
